﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuotationService.HistoryQuotation
{
    internal class FutureEntityConverter :
        IEntityConverter<Quotation.Future.BasicInfo, FutureBasicInfo>,
        IEntityConverter<Quotation.Future.QuotationInfo, FutureQuotationInfo>,
        IEntityConverter<Quotation.Future.TickInfo, FutureTickInfo>,
        IEntityConverter<Quotation.Future.DailyData, FutureDailyData>,
        IEntityConverter<Quotation.Future.MinuteDataCollection, FutureMinuteDataCollection>
    {
        public Quotation.Future.BasicInfo Convert(FutureBasicInfo basicInfo)
        {
            return new Quotation.Future.BasicInfo()
            {
                AccountDate = Quotation.Date.FromYMMDD(basicInfo.AccountDate),
                BeginDate = Quotation.Date.FromYMMDD(basicInfo.BeginDate),
                ContractSerialNo = basicInfo.ContractSerialNo,
                ConvertQty = basicInfo.ConvertQty,
                Decimal = basicInfo.Decimal,
                DynamicVer = basicInfo.DynamicVer,
                EndDate = Quotation.Date.FromYMMDD(basicInfo.EndDate),
                MaxLimitOrderQty = basicInfo.MaxLimitOrderQty,
                MaxMarketOrderQty = basicInfo.MaxMarketOrderQty,
                MaxOrderPrice = basicInfo.MaxOrderPrice,
                MinLimitOrderQty = basicInfo.MinLimitOrderQty,
                MinMarketOrderQty = basicInfo.MinMarketOrderQty,
                MinOrderPrice = basicInfo.MinOrderPrice,
                Name = basicInfo.Name,
                OpenPosition = basicInfo.OpenPosition,
                OpenPrice = basicInfo.OpenPrice,
                OrderPriceUnit = basicInfo.OrderPriceUnit,
                PreClosePrice = basicInfo.PreClosePrice,
                PreExchTotalKnockAmt = basicInfo.PreExchTotalKnockAmt,
                PreExchTotalKnockQty = basicInfo.PreExchTotalKnockQty,
                PreOpenPosition = basicInfo.PreOpenPosition,
                PreSettlementPrice = basicInfo.PreSettlementPrice,
                ProductId = basicInfo.ProductId,
                QuotationTime = basicInfo.QuotationTime,
                SettlementPrice = basicInfo.SettlementPrice,
                StaticVer = basicInfo.StaticVer,
                StkId = new Quotation.SymbolCode(basicInfo.Code),
                TradeType = basicInfo.TradeType,
                StkType = basicInfo.StkType,
                UpdateTime = basicInfo.UpdateTime,
                BigMarginFlag = basicInfo.BigMarginFlag,
                MappingContractId = new Quotation.SymbolCode(basicInfo.MappingContractId),
                QuotLevel = basicInfo.QuotLevel
            };
        }

        public FutureBasicInfo Convert(Quotation.Future.BasicInfo basicInfo)
        {
            return new FutureBasicInfo()
            {
                AccountDate = basicInfo.AccountDate.ToYMMDD(),
                BeginDate = basicInfo.BeginDate.ToYMMDD(),
                ContractSerialNo = basicInfo.ContractSerialNo,
                ConvertQty = basicInfo.ConvertQty,
                Decimal = basicInfo.Decimal,
                DynamicVer = basicInfo.DynamicVer,
                EndDate = basicInfo.EndDate.ToYMMDD(),
                MaxLimitOrderQty = basicInfo.MaxLimitOrderQty,
                MaxMarketOrderQty = basicInfo.MaxMarketOrderQty,
                MaxOrderPrice = basicInfo.MaxOrderPrice,
                MinLimitOrderQty = basicInfo.MinLimitOrderQty,
                MinMarketOrderQty = basicInfo.MinMarketOrderQty,
                MinOrderPrice = basicInfo.MinOrderPrice,
                Name = basicInfo.Name,
                OpenPosition = basicInfo.OpenPosition,
                OpenPrice = basicInfo.OpenPrice,
                OrderPriceUnit = basicInfo.OrderPriceUnit,
                PreClosePrice = basicInfo.PreClosePrice,
                PreExchTotalKnockAmt = basicInfo.PreExchTotalKnockAmt,
                PreExchTotalKnockQty = basicInfo.PreExchTotalKnockQty,
                PreOpenPosition = basicInfo.PreOpenPosition,
                PreSettlementPrice = basicInfo.PreSettlementPrice,
                ProductId = basicInfo.ProductId,
                QuotationTime = basicInfo.QuotationTime,
                SettlementPrice = basicInfo.SettlementPrice,
                StaticVer = basicInfo.StaticVer,
                Code = basicInfo.StkId.LongCode,
                TradeType = basicInfo.TradeType,
                StkType = basicInfo.StkType,
                UpdateTime = basicInfo.UpdateTime,
                BigMarginFlag = basicInfo.BigMarginFlag,
                MappingContractId = basicInfo.MappingContractId.LongCode,
                QuotLevel = basicInfo.QuotLevel
            };
        }

        public Quotation.Future.QuotationInfo Convert(FutureQuotationInfo quotInfo)
        {
            var rtn = new Quotation.Future.QuotationInfo()
            {
                StkId = new Quotation.SymbolCode(quotInfo.Code),
                BuyAvgPrice = quotInfo.BuyAvgPrice,
                SellAvgPrice = quotInfo.SellAvgPrice,
                InnerVolume = quotInfo.InnerVolume,
                OuterVolume = quotInfo.OuterVolume,
                NotDefineVolume = quotInfo.NotDefineVolume,
                Name = quotInfo.Name,
                SN = quotInfo.SN,
                UpdateTime = quotInfo.UpdateTime,
                QuotationTime = quotInfo.QuotationTime,
                Tick = Convert(quotInfo.Tick),
                QuotLevel =quotInfo.QuotLevel,
                BidAsk = new List<Quotation.BidAsk>()
            };
            if (quotInfo.BidAsk != null)
            {
                foreach (var bidAsk in quotInfo.BidAsk)
                {
                    rtn.BidAsk.Add(new Quotation.BidAsk()
                    {
                        Buy = new Quotation.QtyPrice() { Price = bidAsk.BuyPrice, Qty = bidAsk.BuyQty },
                        Sell = new Quotation.QtyPrice() { Price = bidAsk.SellPrice, Qty = bidAsk.SellQty }
                    });
                }
            }
            return rtn;
        }

        public FutureQuotationInfo Convert(Quotation.Future.QuotationInfo quotInfo)
        {
            var rtn = new FutureQuotationInfo()
            {
                Code = quotInfo.StkId.LongCode,
                BuyAvgPrice = quotInfo.BuyAvgPrice,
                SellAvgPrice = quotInfo.SellAvgPrice,
                InnerVolume = quotInfo.InnerVolume,
                OuterVolume = quotInfo.OuterVolume,
                NotDefineVolume = quotInfo.NotDefineVolume,
                Name = quotInfo.Name,
                SN = quotInfo.SN,
                UpdateTime = quotInfo.UpdateTime,
                QuotationTime = quotInfo.QuotationTime,
                Tick = Convert(quotInfo.Tick),
                QuotLevel = quotInfo.QuotLevel,
                BidAsk = new List<OrderBook>()
            };
            if (quotInfo.BidAsk != null)
            {

                foreach (var bidAak in quotInfo.BidAsk)
                {
                    rtn.BidAsk.Add(new OrderBook()
                    {
                        BuyPrice = bidAak.Buy.Price,
                        BuyQty = bidAak.Buy.Qty,
                        SellPrice = bidAak.Sell.Price,
                        SellQty = bidAak.Sell.Qty
                    });
                }
            }
            return rtn;
        }

        public Quotation.Future.TickInfo Convert(FutureTickInfo tickInfo)
        {
            return new Quotation.Future.TickInfo()
            {
                BSType = tickInfo.BSType,
                Diff = tickInfo.Diff,
                DiffRate = tickInfo.DiffRate,
                HighPrice = tickInfo.HighPrice,
                LowPrice = tickInfo.LowPrice,
                OpenPosition = tickInfo.OpenPosition,
                KnockAmt = tickInfo.KnockAmt,
                KnockAvgPrice = tickInfo.KnockAvgPrice,
                KnockPrice = tickInfo.KnockPrice,
                KnockQty = tickInfo.KnockQty,
                KnockTime = Quotation.Time.FromHMMss(tickInfo.KnockTime),
                PositDiff = tickInfo.PositDiff,
                TickSN = tickInfo.TickSN,
                TotalBuyQty = tickInfo.TotalBuyQty,
                TotalBuyQtyDiff = tickInfo.TotalBuyQtyDiff,
                TotalKnockAmt = tickInfo.TotalKnockAmt,
                TotalKnockQty = tickInfo.TotalKnockQty,
                TotalSellQty = tickInfo.TotalSellQty,
                TotalSellQtyDiff = tickInfo.TotalSellQtyDiff,
                VolumeDiff = tickInfo.VolumeDiff,
                VolumeRate = tickInfo.VolumeRate
       
            };
        }

        public FutureTickInfo Convert(Quotation.Future.TickInfo tickInfo)
        {
            return new FutureTickInfo()
            {
                BSType = tickInfo.BSType,
                Diff = tickInfo.Diff,
                DiffRate = tickInfo.DiffRate,
                HighPrice = tickInfo.HighPrice,
                LowPrice = tickInfo.LowPrice,
                OpenPosition = tickInfo.OpenPosition,
                KnockAmt = tickInfo.KnockAmt,
                KnockAvgPrice = tickInfo.KnockAvgPrice,
                KnockPrice = tickInfo.KnockPrice,
                KnockQty = tickInfo.KnockQty,
                KnockTime = tickInfo.KnockTime.ToHMMss(),
                PositDiff = tickInfo.PositDiff,
                TickSN = tickInfo.TickSN,
                TotalBuyQty = tickInfo.TotalBuyQty,
                TotalBuyQtyDiff = tickInfo.TotalBuyQtyDiff,
                TotalKnockAmt = tickInfo.TotalKnockAmt,
                TotalKnockQty = tickInfo.TotalKnockQty,
                TotalSellQty = tickInfo.TotalSellQty,
                TotalSellQtyDiff = tickInfo.TotalSellQtyDiff,
                VolumeDiff = tickInfo.VolumeDiff,
                VolumeRate = tickInfo.VolumeRate
            };
        }

        public Quotation.Future.DailyData Convert(FutureDailyData dailyData)
        {
            return new Quotation.Future.DailyData()
            {
                StkId = new Quotation.SymbolCode(dailyData.Code),
                ClosePrice = dailyData.ClosePrice,
                HighPrice = dailyData.HighPrice,
                LowPrice = dailyData.LowPrice,
                OpenPosition = dailyData.OpenPosition,
                OpenPrice = dailyData.OpenPrice,
                PreSettlementPrice = dailyData.PreSettlementPrice,
                TotalKnockAmt = dailyData.TotalKnockAmt,
                TotalKnockQty = dailyData.TotalKnockQty,
                TradeDay = Quotation.Date.FromYMMDD(dailyData.TradeDay)
            };
        }

        public FutureDailyData Convert(Quotation.Future.DailyData dailyData)
        {
            return new FutureDailyData()
            {
                Code = dailyData.StkId.LongCode,
                ClosePrice = dailyData.ClosePrice,
                HighPrice = dailyData.HighPrice,
                LowPrice = dailyData.LowPrice,
                OpenPosition = dailyData.OpenPosition,
                OpenPrice = dailyData.OpenPrice,
                PreSettlementPrice = dailyData.PreSettlementPrice,
                TotalKnockAmt = dailyData.TotalKnockAmt,
                TotalKnockQty = dailyData.TotalKnockQty,
                TradeDay = dailyData.TradeDay.ToYMMDD()
            };
        }

        public Quotation.Future.MinuteData Convert(FutureMinuteData minuteData, Quotation.SymbolCode stkId)
        {
            return new Quotation.Future.MinuteData()
            {
                StkId = stkId,
                ClosePrice = minuteData.ClosePrice,
                HighPrice = minuteData.HighPrice,
                KnockAvgPrice = minuteData.KnockAvgPrice,
                KnockAmt = minuteData.KnockAmt,
                KnockQty = minuteData.KnockQty,
                LowPrice = minuteData.LowPrice,
                OccurTime = Quotation.Time.FromHMM(minuteData.OccurTime),
                OpenPosition = minuteData.OpenPosition,
                OpenPrice = minuteData.OpenPrice,
                TickSN = minuteData.TickSN
            };
        }

        public FutureMinuteData Convert(Quotation.Future.MinuteData minuteData)
        {
            return new FutureMinuteData()
            {
                ClosePrice = minuteData.ClosePrice,
                HighPrice = minuteData.HighPrice,
                KnockAvgPrice = minuteData.KnockAvgPrice,
                KnockAmt = minuteData.KnockAmt,
                KnockQty = minuteData.KnockQty,
                LowPrice = minuteData.LowPrice,
                OccurTime = (ushort)minuteData.OccurTime.ToHMM(),
                OpenPosition = minuteData.OpenPosition,
                OpenPrice = minuteData.OpenPrice,
                TickSN = minuteData.TickSN
            };
        }

        public Quotation.Future.MinuteDataCollection Convert(FutureMinuteDataCollection minuteDataCollection)
        {
            Quotation.Future.MinuteDataCollection rtn = new Quotation.Future.MinuteDataCollection()
            {
                StkId = new Quotation.SymbolCode(minuteDataCollection.Code),
                TradeDay = Quotation.Date.FromYMMDD(minuteDataCollection.TradeDay),
                MinuteData = new List<Quotation.Future.MinuteData>()
            };
            if (minuteDataCollection.MinuteData != null)
            {
                foreach (var md in minuteDataCollection.MinuteData)
                {
                    rtn.MinuteData.Add(Convert(md, rtn.StkId));
                }
            }

            return rtn;
        }

        public FutureMinuteDataCollection Convert(Quotation.Future.MinuteDataCollection minuteDataCollection)
        {
            FutureMinuteDataCollection rtn = new FutureMinuteDataCollection()
            {
                Code = minuteDataCollection.StkId.LongCode,
                TradeDay = minuteDataCollection.TradeDay.ToYMMDD(),
                MinuteData = new List<FutureMinuteData>()
            };
            if (minuteDataCollection.MinuteData != null)
            {
                foreach (var md in minuteDataCollection.MinuteData)
                {
                    rtn.MinuteData.Add(Convert(md));
                }
            }

            return rtn;
        }
    }
}
